package edu.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.pojo.Member;
import edu.pojo.Sum;
import edu.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ManageController {

    @Autowired
    private MemberService memberService;


    /*
    * 登录
    * */
    @PostMapping("/user/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model, HttpSession session){

        if("root".equals(username) && "175198".equals(password))
        {
            session.setAttribute("user", username);
            return "redirect:/tolist/1";
        }
        else {
            model.addAttribute("msg", "用户名或密码错误!");
        }

        return "login";
    }

    /*
    * 获取指定的页数数据
    * */
    @RequestMapping("/tolist/{idx}")
    public String memberList(@PathVariable("idx") int idx, Model model, HttpSession session) {
        int maxPage, now;

        if(session.getAttribute("maxPage") == null)
        {
            // 总人数
            Sum all = memberService.queryAll();
            // 总页数
            maxPage = all.getCnt() / 15;
            if(all.getCnt() % 15 > 0) maxPage ++;
            // 讲当前总页数放入session
            session.setAttribute("maxPage", maxPage);
        }
        else
        {
            maxPage = (Integer) (session.getAttribute("maxPage"));
        }

        now = idx;
        if(now < 1) now = 1;
        if(now > maxPage) now = maxPage;


        List<Member> members = memberService.queryMember(now);
        model.addAttribute("members", members);
        return "list";
    }

    /*
    * 根据姓名关键字 and id查询
    * */
    @PostMapping("/getkey")
    public String getKey(@RequestParam("key") String key, Model model) {
        List<Member> members = memberService.queryMemberByKey(key);
        model.addAttribute("members", members);
        return "list";
    }

    /*
    * 获取相应id的准确信息
    * */
    @RequestMapping("/godetail/{id}")
    public String goDetail(@PathVariable("id") int id, Model model) {
        Member member = memberService.queryMemberByID(id);
        model.addAttribute("member", member);
        return "detail";
    }

    /*
    * 修改录取信息
    * */
    @RequestMapping("/updatepass")
    public String updatePass(@RequestParam("id") int id, @RequestParam("pass") int pass, Model model) {
        int res = memberService.updatePass(id, pass);

        if(res > 0) return "redirect:/tolist/1";
        else {
            model.addAttribute("msg", "修改失败");
            return "redirect:/godetail/" + id;
        }
    }

    /*
    * app姓名模糊查询接口
    * */
    @ResponseBody
    @RequestMapping("/querybykey/{key}")
    public String wxAppQueryByKey(@PathVariable("key") String key) throws JsonProcessingException {
        List<Member> members = memberService.queryMemberByKey(key);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(members);
    }

    /*
    * 获取待商议人员
    * */
    @RequestMapping("/getunchecked")
    public String getUnchecked(Model model){
        List<Member> members = memberService.queryAllUnchecked();
        model.addAttribute("members", members);
        return "list";
    }

}
